<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class lifting_kkks extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		//$this->output->enable_profiler(true);
		$this->load->model('lifting_kkks_model');
		
		$this->load->model('prosedur_model');
	}
	
	function index(){
		$this->load->view('lifting_kkks_view');
	}
	
	function grid(){
		$data = $this->lifting_kkks_model->getdata();
		echo $this->lifting_kkks_model->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['ID'] = '';
		$data['ID_KPS'] = '';
		$data['TAHUN'] = date('Y');
		$data['BULAN'] = (int) date('m');
		
		$this->load->view('lifting_kkks_add_view',$data);
	}
	
	function edit($kode,$tahun,$bulan){
	  	$r = $this->lifting_kkks_model->getdataedit($kode,$bulan);
		
		$data['ID_KPS'] = $kode;
		//$data['NAMA_KPS'] = $NAMA_KPS;
		$data['NAMA_KPS'] = $r->row()->NAMA_KPS;
		$data['TAHUN'] = $tahun;
		$data['BULAN'] = $bulan;
		$data['BLN'] = $r->row()->BLN;
		$data['aksi'] = 'edit';
		$this->load->view('lifting_kkks_add_view', $data);
	}
	
	function get_list(){
		$id_kps = $this->input->post('ID_KPS');
		$bulan = $this->input->post('BULAN');
		$tahun = $this->input->post('TAHUN');
		
		if($id_kps == '')
			return;
		
		$data = $this->lifting_kkks_model->getdata_detail($id_kps, $bulan, $tahun);
		
		$old_parent = '';
		$new_parent = '';
		$i=1;
		$cur_i = 0;
		$count=0;
		$detail;
		
		
		foreach($data['row_data']->result() as $row){
			$new_parent = $row->NM_PROD;
			if($new_parent == $old_parent){
				$detail[] = array('idx'=>$i, 'NM_PROD'=>$row->WILAYAH, 'isparent'=>false,  
									'CD_PROD'		=>$row->CD_PROD,
									'BAREL'			=>'0',
									'MMBTU'			=>'0',
									'MSCF'			=>'0',
									'MT'			=>'0',
									'GROSS_PRICE'	=>$row->GROSS_PRICE,
									'VOL_PREMIUM'	=>'0',
									'PREMIUM_PRICE'	=>'0',
									'GROSSREV'		=>'0',
									'NETBACK'		=>'0',
									'_parentId'		=>$cur_i);
				$count += 1;
			}else{
				$old_parent = $new_parent;
				$cur_i = $i;
				$detail[] = array('idx'=>$i, 'NM_PROD'=>$row->NM_PROD, 'isparent'=>true);
				$i++;
				$detail[] = array('idx'=>$i, 'NM_PROD'=>$row->WILAYAH, 'isparent'=>false,  
									'CD_PROD'		=>$row->CD_PROD, 
									'BAREL'			=>'0',
									'MMBTU'			=>'0',
									'MSCF'			=>'0',
									'MT'			=>'0',
									'GROSS_PRICE'	=>$row->GROSS_PRICE,
									'VOL_PREMIUM'	=>'0',
									'PREMIUM_PRICE'	=>'0',
									'GROSSREV'		=>'0',
									'NETBACK'		=>'0',
									'_parentId'=>$cur_i);
				$count +=2;
			}
			$i++;
		}
		$out['total'] = $count;
		$out['rows'] = $detail;
		echo json_encode($out);
	}
	# end grid
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		
		$data['pesan_error'] = '';
		
		$result = $this->lifting_kkks_model->UpdateOnDb($data);
		//$data['pesan_error'] = 'kimo';
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode,$ID_KPS,$TAHUN,$BULAN){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		$data['ID'] = $kode;
		$data['ID_KPS'] = $ID_KPS;
        $data['TAHUN'] = $TAHUN;
        $data['BULAN'] = $BULAN;


		// cek data di tabel realiasi revenue -> T_lifting_kkks_REVENUE
		if(!$this->lifting_kkks_model->EXIST_T_lifting_kkks_REVENUE($data)){
		
			$result = $this->lifting_kkks_model->DeleteOnDb($kode);
			if(!$result){
				$data['pesan_error'] .= 'Gagal delete!';
			}
		}else{
			$data['pesan_error'] .= 'Data Sudah di lifting_kkks';
		}
				
		//$result = $this->lifting_kkks_model->DeleteOnDb($kode);
		
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
}